package com.quanquan.management.controller;

import DTO.PageDTO;
import com.quanquan.management.dto.AbnormalConsumptionDTO;
import com.quanquan.management.request.AbnormalConsumptionRequest;
import com.quanquan.management.service.AbnormalConsumptionService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import web.response.ResultBody;

/**
 * @ClassName:AbnormalConsumptionController
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/8/26 10:16
 * @Version 1.0
 */
@Controller
@RequestMapping("/abnormal_consumption")
public class AbnormalConsumptionController extends ManagerBaseController {

    @Autowired
    private AbnormalConsumptionService abnormalConsumptionService;

    /**
     * 多条件查询 优惠券列表
     * @param pageSize
     * @param page
     * @param keyword
     * @param sort
     * @param fromDate
     * @param toDate
     * @param isAsc
     * @param sta
     * @param hot
     * @param ckSta
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String sort,
                       @RequestParam(required = false) String fromDate,
                       @RequestParam(required = false) String toDate,
                       @RequestParam(required = false) boolean isAsc,
                       @RequestParam(required = false) Integer sta,
                       @RequestParam(required = false) Integer hot,
                       @RequestParam(required = false) Integer ckSta,
                       Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0) {
            page = page - 1;
        }
        AbnormalConsumptionRequest requestBody = new AbnormalConsumptionRequest(page, pageSize, keyword, sort, isAsc);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        PageDTO<AbnormalConsumptionDTO> pageDTO = abnormalConsumptionService.list(requestBody);
        model.addAttribute("pageObj", pageDTO);
        return "abnormalConsumption/list";
    }

    /**
     * 导出发放日志
     *
     * @param pageSize
     * @param page
     * @param keyword
     * @param sort
     * @param fromDate
     * @param toDate
     * @param isAsc
     * @return
     */
    @RequestMapping("/export")
    public ResultBody exportSendRecord(@RequestParam(required = false) Integer pageSize,
                                       @RequestParam(required = false) Integer page,
                                       @RequestParam(required = false) String keyword,
                                       @RequestParam(required = false) String sort,
                                       @RequestParam(required = false) String fromDate,
                                       @RequestParam(required = false) String toDate,
                                       @RequestParam(required = false) Integer total,
                                       @RequestParam(required = false) boolean isAsc) {

        AbnormalConsumptionRequest requestBody = new AbnormalConsumptionRequest(page, pageSize, keyword, sort, isAsc);
        requestBody.setSize(total);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        try{
            abnormalConsumptionService.exportSendCouponRecord(this.response,requestBody);
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg());
        }catch (Exception e){
            e.printStackTrace();
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
        }
    }


}
